<template>
  <div>
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="菜单" prop="name">
        <el-input v-model="ruleForm.name"></el-input>
      </el-form-item>
      <el-form-item label="图标" prop="icon">
        <el-select v-model="ruleForm.icon" placeholder="请选择" style="width: 100%">
          <el-option v-for="item in icons" :key="item.id" :label="item.name" :value="item.icon">
            <template slot-scope="scoped">
              <span style="font-size: 14px" :class="`iconfont ${item.icon}`"> {{ item.name }}</span>
            </template>
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="是否有子菜单">
        <el-switch v-model="menuLevel"></el-switch>
      </el-form-item>
      <el-form-item v-if="!menuLevel" label="路径" prop="path">
        <el-input v-model="ruleForm.path"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="medium" @click="onSubmit('ruleForm')">提交</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { iconList } from '@/api/menuApi.js'
export default {
  data() {
    return {
      icons: [],
      menuLevel: false,
      rules: {
        name: [
          { required: true, message: '请输入菜单名称', trigger: 'blur' },
          { min: 3, max: 9, message: '长度在 3 到 9 个字符', trigger: 'blur' },
        ],
      },
    }
  },
  props: ['ruleForm'],
  created() {
    this.getIcon()
  },
  methods: {
    getIcon() {
      iconList().then((res) => {
        this.icons = res.data.data
        console.log(this.icons)
      })
    },
    onSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$emit('submit', this.ruleForm)
        } else {
          this.$message.warning('请按表单要求填写完整！！！')
          return false
        }
      })
    },
    resetForm() {
      this.$refs.ruleForm.resetFields()
    },
  },
}
</script>

<style lang="less" scoped>
</style>